const moment = require('moment-timezone');

function isDSTOverlap(dateTime, timeZone) {
  // 将给定的时间转换为moment对象，并设定其时区
  let time = moment.tz(dateTime, null, timeZone);
  // 为了检测重叠，观察当前时间向前和向后推移一小时后的时间是否还在DST中
  let beforeHour = time.clone().subtract(1, 'hour');
  let afterHour = time.clone().add(1, 'hour');
  // 检查当前时间与前后各一小时的DST状态是否有变化
  // 如果在当前时间是DST结束的第一个小时内，那么向前推一个小时将会退出DST模式
  return time.isDST() && beforeHour.isDST() && !afterHour.isDST();
}

function isTimestamp(value) {
  const num = Number(value);
  return !isNaN(num) && isValidTimestamp(num);
}
function isValidTimestamp(value) {
  if (!Number.isFinite(value)) {
    return false;
  }
  const date = new Date(value);
  return !isNaN(date.getTime());
}

function handleDealDataToStr(dataParam, timeZone) {
  let isDST;
  let resultLabel;
  let suffixLabel = ' (DST)';
  let formatTmp = 'YYYY-MM-DD HH:mm:ss';
  //   let formatTmp = "YYYY-MM-DD";
  if (
    (dataParam instanceof Date && !isNaN(dataParam.getTime())) ||
    moment.isMoment(dataParam)
  ) {
    isDST = moment.tz(dataParam, null, timeZone).clone().isDST();
    resultLabel = moment.tz(dataParam, null, timeZone).format(formatTmp);
    resultLabel = isDST ? `${resultLabel}${suffixLabel}` : `${resultLabel}`;
  } else if (Object.prototype.toString.call(dataParam) === '[object String]') {
    dataParam = dataParam.trim();
    if (dataParam.indexOf(suffixLabel) > -1) {
      let dataParamTmp = dataParam.replace(suffixLabel, '');
      isDST = moment.tz(dataParamTmp, null, timeZone).clone().isDST();
      let formatLabel = moment(dataParamTmp).format(formatTmp);
      resultLabel = isDST ? `${formatLabel}${suffixLabel}` : formatLabel;
    } else {
      isDST = moment.tz(dataParam, null, timeZone).clone().isDST();
      let overlapTmp = isDSTOverlap(dataParam, timeZone);
      resultLabel = moment.tz(dataParam, null, timeZone).format(formatTmp);
      resultLabel =
        isDST && !overlapTmp
          ? `${resultLabel}${suffixLabel}`
          : `${resultLabel}`;
    }
  } else if (isTimestamp(dataParam)) {
    dataParam =
      dataParam.toString().length === 13 ? dataParam : dataParam * 1000;
    isDST = moment.tz(dataParam, null, timeZone).clone().isDST();
    resultLabel = moment.tz(dataParam, null, timeZone).format(formatTmp);
    resultLabel = isDST ? `${resultLabel}${suffixLabel}` : `${resultLabel}`;
  }
  return resultLabel;
}

let timeZone = 'Australia/Sydney';
let dataParam = moment.tz('2024-04-06 15:50:00', null, 'UTC');

let resultDateDST = handleDealDataToStr(dataParam.clone(), timeZone);
console.log('resultDateDST', resultDateDST);

let otherUTC = moment.tz('2024-04-06 16:50:00', null, 'UTC');
let resultDate = handleDealDataToStr(otherUTC.clone(), timeZone);
console.log('resultDate', resultDate);

let resultDSTStr = handleDealDataToStr('2024-04-07 02:50:00 (DST)', timeZone);
console.log('resultDSTStr', resultDSTStr);

let resultStr = handleDealDataToStr('2024-04-07 02:50:00', timeZone);
console.log('resultStr', resultStr);

let resultNumberDST = handleDealDataToStr(dataParam.valueOf(), timeZone);
console.log('resultNumber', resultNumberDST);

let resultNumber = handleDealDataToStr(
  dataParam.valueOf() + 60 * 60 * 1000,
  timeZone,
);
console.log('resultNumber', resultNumber);

// 字符串转UTC
function handleStrToUTC(sourceStr, timeZone) {
  let suffixLabel = ' (DST)';
  let resultUtc;
  sourceStr = sourceStr.trim();
  if (sourceStr.indexOf(suffixLabel) > -1) {
    let sourceStrTmp = sourceStr.replace(suffixLabel, '');
    resultUtc = moment.tz(sourceStrTmp, null, timeZone).utc();
  } else {
    if (isDSTOverlap(sourceStr, timeZone)) {
      resultUtc = moment.tz(sourceStr, null, timeZone).add(1, 'hour').utc();
    } else {
      resultUtc = moment.tz(sourceStr, null, timeZone).utc();
    }
  }

  return resultUtc;
}
console.log(
  'handleStrToUTC',
  handleStrToUTC('2024-04-07 02:50:00 (DST)', timeZone),
  moment
    .tz(handleStrToUTC('2024-04-07 02:50:00', timeZone), null, timeZone)
    .format('YYYY-MM-DD HH:mm:ss'),
);
